#include "stack_linklist.h"

int Stack_LinkList_init(Stack_LinkList_t ** top){
    if(*top!=NULL){
        return -1;
    }
    (*top) = (Stack_LinkList_t*)malloc(sizeof(Stack_LinkList_t));
    if((*top)==NULL){
        LOG("stack create failed\n");
        return -1;
    }
    (*top)->next = NULL;
    return 0;
}

int Stack_LinkList_push(Stack_LinkList_t * top,int data){
    if(top==NULL){
        return -1;
    }
    Stack_LinkList_t * p =(Stack_LinkList_t *)malloc(sizeof(Stack_LinkList_t));
    if(p==NULL){
        LOG("push failed\n");
        return -1;
    }
    p->data = data;
    p->next = top->next;
    top->next = p;
    return 0;
}

int Stack_LinkList_isNULL(Stack_LinkList_t *top){
    if(top->next == NULL){
        return 0;
    }
    return -1;
}

int Stack_LinkList_pull(Stack_LinkList_t * top,Stack_LinkList_t *item){
    if(Stack_LinkList_isNULL(top)==0){
        return -1;
    }
    Stack_LinkList_t * p = top->next;
    memcpy(item,p,sizeof(Stack_LinkList_t));
    top->next = p->next;
    free(p);
    p = NULL;
    return 0;
}

int Stack_LinkList_select(Stack_LinkList_t *top){
    if(Stack_LinkList_isNULL(top)==0){
        return -1;
    }
    Stack_LinkList_t * p = top->next;
    while(p!=NULL){
        printf("%d->",p->data);
        p = p ->next;
    }
    printf("\n");
    return 0;
}

int Stack_LinkList_destory(Stack_LinkList_t *top){
    if(top == NULL){
        return -1;
    }
    Stack_LinkList_t * p = top->next;
    while (p!=NULL)
    {
        Stack_LinkList_t * q = p->next;
        free(p);
        p = q;
    }
    free(top);
    top = NULL;
    return 0;
}